<?php

namespace App\Models\Statistics;

use App\Models\Subscribe;
use App\Models\Order;


class Trade
{
    public function getTransactionData($type,$start_time, $end_time)
    {
        switch ($type){
            case '1':
                //昨天 2019-01-20 00:00:00至2019-01-20 23:59:59
                $start_time = strtotime(date("Y-m-d 00:00:00")) - 86400;
                $end_time   = strtotime(date("Y-m-d 00:00:00")) - 1;
                break;
            case '2':
                //近7天 2019-01-14 08:56:00至2019-01-21 08:56:00
                $start_time = strtotime('-7 day', time());
                $end_time   = time();
                break;
            case '3':
                //近30天 2019-01-14 08:56:00至2019-01-21 08:56:00
                $start_time = strtotime('-30 day', time());
                $end_time   = time();
                break;
            default :
                //自定义时间 如果没设置默认昨天
                $start_time = isset($start_time) && $start_time ? strtotime($start_time) : strtotime(date("Y-m-d 00:00:00"))-86400;
                $end_time   = isset($end_time) && $end_time ? strtotime($end_time) : strtotime(date("Y-m-d 00:00:00"))-1;
                break;
        }

        $data['experience_count'] = $this->getExperienceCount($start_time, $end_time);
        $data['subscribe_count']  = $this->getSubscribeCount($start_time, $end_time);
        $data['order_count']      = $this->getOrderCount($start_time, $end_time);
        $data['order_user_count'] = $this->getOrderUserCount($start_time, $end_time);
        $data['vip_count']        = $this->getVipCount($start_time, $end_time);
        $data['xb_amount']        = $this->getXbAmount($start_time, $end_time);
        $data['order_amount']     = $this->getOrderAmount($start_time, $end_time);
        $data['conversion_rate']  = $data['order_count'] ? number_format($data['order_user_count'] * 100 / $data['order_count'],'2','.','') . '%' : '0.00%';

        /*$data['type']       = $type;
        $data['start_time'] = date('Y-m-d H:i:s', $start_time);
        $data['end_time']   = date('Y-m-d H:i:s', $end_time);*/

        return $data;
    }

    //获取体验人数
    public function getExperienceCount($start_time, $end_time)
    {
        //jxt_subscribe_status
        return Subscribe\Status::where([
            ['subscribe_time', '>=', $start_time],
            ['subscribe_time', '<=', $end_time]
        ])->groupBy('user_id')->count();
    }

    //获取订阅人数
    public function getSubscribeCount($start_time, $end_time)
    {
        return Subscribe\Info::where([
            ['status','1'],
            ['subscribe_time', '>=', $start_time],
            ['subscribe_time', '<=', $end_time]
        ])->groupBy('user_id')->count();
    }

    //获取下单数
    public function getOrderCount($start_time, $end_time)
    {
        return Order\Info::where([
            ['pay_time', '>=', $start_time],
            ['pay_time', '<=', $end_time]
        ])->count();
    }

    //下单付款单数
    public function getOrderUserCount($start_time, $end_time)
    {
        return Order\Info::where([
            ['pay_status','2'],
            ['pay_time', '>=', $start_time],
            ['pay_time', '<=', $end_time]
        ])->count();
    }

    //vip充值人数
    public function getVipCount($start_time, $end_time)
    {
        return Order\Info::where([
            ['pay_status','2'],
            ['type','3'],
            ['vip_id','>',0],
            ['pay_time','>=', $start_time],
            ['pay_time', '<=', $end_time]
        ])->count();
    }

    //购买校币金额
    public function getXbAmount($start_time, $end_time)
    {
        return Order\Info::where([
            ['pay_status','2'],
            ['type','2'],
            ['pay_time','>=', $start_time],
            ['pay_time', '<=', $end_time]
        ])->sum('total_fee');
    }

    //下单总金额
    public function getOrderAmount($start_time, $end_time)
    {
        return Order\Info::where([
            ['pay_status','2'],
            ['pay_time','>=', $start_time],
            ['pay_time', '<=', $end_time]
        ])->sum('total_fee');
    }

    //获取销售额占比
    public function getSalesVolume($days_type, $sales_type, $start_time, $end_time)
    {
        switch ($days_type){
            case '1':
                //昨天 2019-01-20 00:00:00至2019-01-20 23:59:59
                $start_time = strtotime(date("Y-m-d 00:00:00",time())) - 86400;
                $end_time   = strtotime(date("Y-m-d 00:00:00",time())) - 1;
                break;
            case '2':
                //近7天 2019-01-14 08:56:00至2019-01-21 08:56:00
                $start_time = strtotime('-7 day',time());
                $end_time   = time();
                break;
            case '3':
                //近30天 2019-01-14 08:56:00至2019-01-21 08:56:00
                $start_time = strtotime('-30 day');
                $end_time   = time();
                break;
            default :
                //自定义时间 如果没设置默认昨天
                $start_time = isset($start_time) && $start_time ? strtotime($start_time) : strtotime(date("Y-m-d 00:00:00",time()))-86400;
                $end_time   = isset($end_time) && $end_time ? strtotime($end_time) : strtotime(date("Y-m-d 00:00:00",time()))-1;
                break;
        }

        $order_amount = Order\Info::where([
            ['pay_status','2'],
            ['pay_time','>=', $start_time],
            ['pay_time', '<=', $end_time]
        ])->sum('total_fee');

        $vip_amount = Order\Info::leftJoin('jxt_member_price as b','b.id', 'jxt_order_info.vip_id')
            ->when($sales_type,function ($query) use ($sales_type) {
                return $query->where('jxt_order_info.mobile_type',$sales_type);
            })->when($start_time,function ($query) use ($start_time) {
                return $query->where('jxt_order_info.pay_time', '>=', $start_time);
            })->when($end_time,function ($query) use ($end_time) {
                return $query->where('jxt_order_info.pay_time', '<=', $end_time);
            })->where([
                ['pay_status','2'],
                ['jxt_order_info.pay_status','2'],
                ['jxt_order_info.type', '3'],
                ['jxt_order_info.vip_id', '>', '0'],
            ])->groupBy('vip_id')
                ->selectRaw('sum(jxt_order_info.total_fee) as value,b.name')
                ->get()
                ->toArray();

        $subscribe_amount = Order\Info::leftJoin('jxt_order_subscribe as b','b.order_id','jxt_order_info.id')
            ->when($sales_type,function ($query) use ($sales_type) {
                return $query->where('jxt_order_info.mobile_type',$sales_type);
            })->when($start_time,function ($query) use ($start_time) {
                return $query->where('jxt_order_info.pay_time', '>=', $start_time);
            })->when($end_time,function ($query) use ($end_time) {
                return $query->where('jxt_order_info.pay_time', '<=', $end_time);
            })->where([
                ['pay_status','2'],
                ['jxt_order_info.pay_status','2'],
                ['jxt_order_info.type', '1']
            ])->groupBy('b.subscribe_id')
                ->selectRaw('sum(jxt_order_info.total_fee) as value,b.monthNum as name')
                ->get()
                ->toArray();


        $data = [];
        $total = 0;
        foreach ($vip_amount as $key=>$val){
            $total += $val['value'];

            $data[] = [
                'value' => $val['value'],
                'name'  => '老师'.$val['name']
            ];
        }

        foreach ($subscribe_amount as $key=>$val){
            $total += $val['value'];

            $data[] = [
                'value' => $val['value'],
                'name'  => '家长订阅 '.$val['name'].'个月'
            ];
        }

        if ($order_amount > $total){
            $data[] = [
                'name' => '其他',
                'value' => number_format(($order_amount * 1) - ($total * 1),'2','.','')
            ];
        }

        return $data;

    }

    public function getCustomerData($mouth_type, $mouth = '')
    {
        //$mouth_type =1本月 =2上月 =3自定义月份$mouth=2018-08
        switch ($mouth_type){

            case '2':
                //上月 2018-12-01 00:00:00至2018-12-31 23:59:59
                $date = date("Y-m-01 00:00:00",strtotime("-1 month",time()));
                $first_day = strtotime($date);
                $end_day   = strtotime(date('Y-m-d 23:59:59',strtotime("$date +1 month -1 day")));

                //上上个月
                $date = date("Y-m-01 00:00:00",strtotime("-2 month",time()));
                $last_first_day = strtotime($date);
                $last_end_day = strtotime(date('Y-m-d 23:59:59',strtotime("$date +1 month -1 day")));

                break;

            case '3':
                //自定义月份
                if ($mouth){
                    $date = $mouth . '-01 00:00:00';
                } else {
                    $date = date("Y-m-01 00:00:00",time());
                }

                //当月
                $first_day = strtotime($date);
                $end_day = strtotime(date('Y-m-d 23:59:59',strtotime("$date +1 month -1 day")));

                //上月
                $last_first_day = strtotime(date("Y-m-01 00:00:00",strtotime("-1 month",$first_day)));
                $last_end_day = $first_day - 1;

                break;

            case '1':
            default :
                //本月 2019-01-01 00:00:00至2019-01-21 16:30:59
                $first_day = strtotime(date("Y-m-01",time()));
                $end_day   = time();

                //上月
                $last_first_day = strtotime(date("Y-m-01 00:00:00",strtotime("-1 month",time())));
                $last_end_day = $first_day - 1;

                break;
        }



        /*dd([
            date("Y-m-d H:i:s",$first_day),
            date("Y-m-d H:i:s",$end_day),
            date("Y-m-d H:i:s",$last_first_day),
            date("Y-m-d H:i:s",$last_end_day),
        ]);*/

        dd([
            $first_day,$end_day,$last_first_day,$last_end_day
        ]);



    }

    function getLastMonthDays($date)
    {
        $first_day = strtotime($date);
        $last_day = strtotime(date('Y-m-d 23:59:59',strtotime("$date +1 month -1 day")));

        return [$first_day,$last_day];
    }


}
